using UnityEngine;
using System.Collections;

public class cLoadPathManager : MonoBehaviour {
	
	public cPathPoint []PathArray;
	
	// Use this for initialization
	void Start () {
		SortPathByIndex();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	
	/*
	 * index를 이용한 소트 내림차순.
	 */
	private void SortPathByIndex()
	{
		
		System.Array.Sort(PathArray,cPathPoint.sortIndex());
		
	}
	
	public cPathPoint GetNearPath( Vector3 pos, int index_no )
	{
		if ( PathArray.Length <= index_no )
			index_no = -1;
		
		float fdistance = 1000000f;
		int selectIndex = 0;
		
		/*
		 * 주어진 인덱스보다 5개 까지 앞쪽의 path를 찾아본다.
		 */
		for ( int i = index_no + 1 ; i < PathArray.Length && i < index_no + 5 ; i++ )
		{
			
			float fTemp = Vector3.Distance( PathArray[i].vPosition, pos );
			if ( fTemp < fdistance )
			{
				
				fdistance = fTemp;
				selectIndex = i;
				
			}
			
		}
		
		return PathArray[selectIndex];
		
//		throw new System.ArgumentException("out of length");
	}
	
}
